﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 三维多段线
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUPolyline3dRef 的包装</remarks>
    public class Polyline3D : Entity
    {
        #region API 函数
        /// <summary>
        /// 创建一个多段线
        /// </summary>
        /// <param name="polyline"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUPolyline3dCreate(out IntPtr polyline);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="polyline"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUPolyline3dRelease(ref IntPtr polyline);

        /// <summary>
        /// 添加端点
        /// </summary>
        /// <param name="polyline"></param>
        /// <param name="num_points"></param>
        /// <param name="points"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUPolyline3dAddPoints(IntPtr polyline, long num_points, Point3D[] points);

        /// <summary>
        /// 获取端点个数
        /// </summary>
        /// <param name="line"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUPolyline3dGetNumPoints(IntPtr line, out long count);

        /// <summary>
        /// 获取全部端点
        /// </summary>
        /// <param name="line"></param>
        /// <param name="len"></param>
        /// <param name="points"></param>
        /// <param name="realcount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUPolyline3dGetPoints(IntPtr line, long len, Point3D[] points, out long realcount);
        #endregion

        /// <summary>
        /// 获取全部端点
        /// </summary>
        public List<Point3D> Points
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUPolyline3dGetNumPoints(Id, out long count));
                Point3D[] points = new Point3D[count];
                SketchUpExceptionHelper.ThrowSUResult(SUPolyline3dGetPoints(Id, count, points, out long realcount));
                return points.ToList();
            }
        }

        internal Polyline3D(IntPtr id) : base(id) { }

        /// <summary>
        /// 创建一个多段线
        /// </summary>
        public Polyline3D()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUPolyline3dCreate(out Id));
        }

        /// <summary>
        /// 使用端点创建多段线
        /// </summary>
        /// <param name="points"></param>
        /// <returns></returns>
        public static Polyline3D Create(params Point3D[] points)
        {
            Polyline3D polyline = new Polyline3D();
            polyline.AddPoints(points);
            return polyline;
        }

        /// <summary>
        /// 添加端点
        /// </summary>
        /// <param name="point"></param>
        public void AddPoints(params Point3D[] point)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUPolyline3dAddPoints(Id, point.Length, point));
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUPolyline3dRelease(ref Id));
        }
    }
}